<?php
/**
 * Projects functions and definitions
 *
 */

if ( !function_exists( 'project_posttype_init' ) ) {
	function project_posttype_init() {

		$project_labels = array(
			'name' => _x('Projects', 'post type general name'),
			'singular_name' => _x('Project', 'post type singular name'),
			'add_new' => _x('Add New', 'project'),
			'add_new_item' => __('Add New Project'),
			'edit_item' => __('Edit Project'),
			'new_item' => __('New Project'),
			'all_items' => __('All Projects'),
			'view_item' => __('View Project'),
			'search_items' => __('Search Projects'),
			'not_found' =>  __('No projects found'),
			'not_found_in_trash' => __('No projects found in Trash'), 
			'parent_item_colon' => '',
			'menu_name' => __('Projects')

		);
		$project_args = array(
			'labels' => $project_labels,
			'public' => true,
			'publicly_queryable' => true,
			'show_ui' => true, 
			'show_in_menu' => true, 
			'query_var' => true,
			'rewrite' => true,
			'capability_type' => 'post',
			'has_archive' => true, 
			'hierarchical' => false,
			'menu_position' => '5.2',
			'menu_icon' => CINGLE_URI . 'img/posttype-project.png',
			'supports' => array( 'title', 'editor' )
		); 
		register_post_type( 'project', $project_args );

	}

	add_action( 'init', 'project_posttype_init' );
}


if ( !function_exists( 'project_taxonomies_innit' ) ) {
	function project_taxonomies_innit() {
		// Project Category
		$labels = array(
			'name' => _x( 'Categories', 'taxonomy general name' ),
			'singular_name' => _x( 'Category', 'taxonomy singular name' ),
			'search_items' =>  __( 'Search Categories' ),
			'all_items' => __( 'All Categories' ),
			'parent_item' => __( 'Parent Category' ),
			'parent_item_colon' => __( 'Parent Category:' ),
			'edit_item' => __( 'Edit Category' ), 
			'update_item' => __( 'Update Category' ),
			'add_new_item' => __( 'Add New Category' ),
			'new_item_name' => __( 'New Category Name' ),
			'menu_name' => __( 'Category' ),
		); 	

		register_taxonomy('project-category',array('project'), array(
			'hierarchical' => true,
			'labels' => $labels,
			'show_ui' => true,
			'query_var' => true,
			'rewrite' => array( 'slug' => 'project-category' ),
		));

		// Project Tags
		$labels = array(
			'name' => _x( 'Tags', 'taxonomy general name' ),
			'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
			'search_items' =>  __( 'Search Tags' ),
			'popular_items' => __( 'Popular Tags' ),
			'all_items' => __( 'All Tags' ),
			'parent_item' => null,
			'parent_item_colon' => null,
			'edit_item' => __( 'Edit Tag' ), 
			'update_item' => __( 'Update Tag' ),
			'add_new_item' => __( 'Add New Tag' ),
			'new_item_name' => __( 'New Tag Name' ),
			'separate_items_with_commas' => __( 'Separate tags with commas' ),
			'add_or_remove_items' => __( 'Add or remove tags' ),
			'choose_from_most_used' => __( 'Choose from the most used tags' ),
			'menu_name' => __( 'Tags' ),
		); 

		register_taxonomy('project-tag','project',array(
			'hierarchical' => false,
			'labels' => $labels,
			'show_ui' => true,
			'update_count_callback' => '_update_post_term_count',
			'query_var' => true,
			'rewrite' => array( 'slug' => 'project-tag' ),
		));
	}
	add_action( 'init', 'project_taxonomies_innit', 0 );
}


// Add metabox
function project_metabox(){
	add_meta_box('project-metabox', 'Project Data', 'project_metabox_callback', 'project', 'normal');
}
add_action('admin_init', 'project_metabox');

// Metabox callback
function project_metabox_callback() { ?>
	<input type="hidden" name="dw_simplex_post_id" id="dw_simplex_post_id" value="<?php echo get_the_ID(); ?>" />
	<table width="100%">
		<tr>
			<th class="left"><?php _e('Project URL','dw-simplex');  ?></th>
			<td>
				<p><input type="text" name="project-url" id="project-url" class="regular-text" value="<?php echo get_post_meta(get_the_ID(), '_project_url', true); ?>" ></p>
			</td>
		</tr>
		<tr class="alternate">
			<th class="left"><?php _e('Client Name','dw-simplex');  ?></th>
			<td>
				<p><input type="text" name="project-client" id="project-client" class="regular-text" value="<?php echo get_post_meta(get_the_ID(), '_project_client', true); ?>" ></p>
			</td>
		</tr>
		<tr>
			<th class="left"><?php _e('Project Date','dw-simplex');  ?></th>
			<td>
				<p><input type="text" name="project-date" id="project-date" class="regular-text" value="<?php echo get_post_meta(get_the_ID(), '_project_date', true); ?>" ></p>
			</td>
		</tr>
		<tr class="alternate">
			<th class="left"><?php _e('Project Skills','dw-simplex');  ?></th>
			<td>
				<p><input type="text" name="project-skill" id="project-skill" class="regular-text" value="<?php echo get_post_meta(get_the_ID(), '_project_skill', true); ?>" ></p>
			</td>
		</tr>			
		<tr>
			<th class="left"><?php _e('Project Images','dw-simplex') ?></th>
			<td>
				<p>
					<input type="text" id="project-thumbnail-small" name="project-thumbnail-small" class="regular-text" value="<?php echo get_post_meta(get_the_ID(), '_project_thumbnail_small', true); ?>" />
					<button onclick="return false" class="simplex_upload_btn button" rel="#project-thumbnail-small" ><?php _e('Upload image','dw-simplex'); ?></button><br />
					<span class="hint"><?php _e('Small size - Recommend width: 210px', 'dw-simplex'); ?></span>
				</p>
				<p>
					<input type="text" id="project-thumbnail-medium" name="project-thumbnail-medium" class="regular-text" value="<?php echo get_post_meta(get_the_ID(), '_project_thumbnail_medium', true); ?>" />
					<button onclick="return false" class="simplex_upload_btn button" rel="#project-thumbnail-medium" ><?php _e('Upload image','dw-simplex'); ?></button><br />
					<span class="hint"><?php _e('Medium size - Recommend width: 290px', 'dw-simplex'); ?></span>
				</p>
				<p>
					<input type="text" id="project-thumbnail-large" name="project-thumbnail-large" class="regular-text" value="<?php echo get_post_meta(get_the_ID(), '_project_thumbnail_large', true); ?>" />
					<button onclick="return false" class="simplex_upload_btn button" rel="#project-thumbnail-large" ><?php _e('Upload image', 'dw-simplex'); ?></button><br />
					<span class="hint"><?php _e('Large size - Recommend width: 400px', 'dw-simplex'); ?></span>
				</p>
			</td>
		</tr>

		<tr class="alternate">
			<th class="left"><?php _e('Display Single Project as'); ?></th>
			<?php $option = get_post_meta(get_the_ID(), '_project_display_option', true); ?>
			<td>
				<select name="project-display-option" id="project-display-option">
					<option <?php selected($option, 'video'); ?>  value="video"><?php _e('Video', 'dw-simplex'); ?></option>
					<option <?php selected($option, 'images'); ?> value="images"><?php _e('Images', 'dw-simplex'); ?></option>
				</select>
			</td>
		</tr>
		<tr id="project-images-option" class="<?php if( 'video' == $option ) echo 'hide'; ?> ">
			<th class="left"><?php _e('Project Gallery', 'dw-simplex'); ?></th>
			<td>
	<?php	
	$slide_images = get_post_meta(get_the_ID(), '_simplex_slide_images', true);
	if( !empty($slide_images) ){
		$i = 0;
		foreach ($slide_images as $image_url) {
			$i++;
			?>
	<p>
		<label class="slide_image_field">
			<input type="text" id="theme_slide_<?php echo $i; ?>" name="theme_slide[<?php echo $i; ?>]" value="<?php echo $image_url; ?>" class="regular-text">
		</label>
		<button onclick="return false" class="simplex_upload_btn button" rel="#theme_slide_<?php echo $i; ?>" ><?php _e('Upload image'); ?></button> <a class="button dw-simplex-remove-slide-image" href="<?php echo $image_url; ?>" alt="<?php echo get_the_ID(); ?>">x</a>
	</p>
			<?php
		}
	} else { ?>
	<p>
		<label class="slide_image_field">
			<input type="text" id="theme_slide_1" name="theme_slide[1]" class="regular-text">
		</label>
		<button onclick="return false" class="simplex_upload_btn button" rel="#theme_slide_1" ><?php _e('Upload image', 'dw-simplex'); ?></button>
	</p>
	<?php } ?>
	<p><button class="button simplex-addnew-slide-image-field"><?php _e('Add new'); ?></button></p>
			</td>
		</tr>
		<tr id="project-video-option" class="<?php if( 'images' == $option || empty($option) ) echo 'hide'; ?>">
			<th class="left"><?php _e('Project Video', 'dw-simplex') ?></th>
			<td><input type="text" name="project-video-url" id="project-video-url" class="regular-text" value="<?php echo get_post_meta(get_the_ID(), '_project_video_url', true); ?>" /><br>
				<span class="hint"><?php _e('Your video <strong>embed</strong> link ( Youtube, Vimeo, ..).Eg: <i>http://www.youtube.com/embed/9bZkp7q19f0</i>', 'dw-simplex'); ?></span>
				<br>
			</td>
		</tr>
	</table>
<?php }


// Action when save post
function admin_save_project($post_id){
	if( !empty($_POST['project-url']) ){
		update_post_meta($post_id, '_project_url', $_POST['project-url']);
	}
	if( !empty($_POST['project-date']) ){
		update_post_meta($post_id, '_project_date', $_POST['project-date']);
	}
	if( !empty($_POST['project-skill']) ){
		update_post_meta($post_id, '_project_skill', $_POST['project-skill']);
	}
	if( !empty($_POST['project-client']) ){
		update_post_meta($post_id, '_project_client', $_POST['project-client']);
	}
	if( !empty($_POST['project-thumbnail-small']) ){
		update_post_meta($post_id, '_project_thumbnail_small', $_POST['project-thumbnail-small']);
	}
	if( !empty($_POST['project-thumbnail-medium']) ){
		update_post_meta($post_id, '_project_thumbnail_medium', $_POST['project-thumbnail-medium']);
	}
	if( !empty($_POST['project-thumbnail-large']) ){
		update_post_meta($post_id, '_project_thumbnail_large', $_POST['project-thumbnail-large']);
	}
	if( !empty($_POST['theme_slide']) ){
		$slide_images = array();
		foreach ($_POST['theme_slide'] as $image_url ) {
			$slide_images[]	= 	$image_url;
		}
		update_post_meta($post_id, '_simplex_slide_images', $slide_images);
	}
	if( !empty($_POST['project-video-url']) ){
		update_post_meta($post_id, '_project_video_url', $_POST['project-video-url']);
	}
	if( !empty($_POST['project-display-option']) ){
		update_post_meta($post_id, '_project_display_option', $_POST['project-display-option']);
	}
}
add_action('save_post', 'admin_save_project');

// Actions meta box
function project_metabox_actions(){
	$result = new stdClass();
	
	if( empty($_POST['post_id']) || empty($_POST['image_url']) ){
		$result->status = 'error';
	} else {
		$slide_images = get_post_meta($_POST['post_id'], '_simplex_slide_images', true);

		foreach ($slide_images as $key => $value) {
				if( $value == $_POST['image_url'] ){
					unset($slide_images[$key]);
					$result->status = 'success';
				}
			}
		update_post_meta($_POST['post_id'], '_simplex_slide_images', $slide_images);	
	}
	echo json_encode($result);
	exit(0);
		
}
add_action('wp_ajax_dw_simplex_remove_slide_image', 'project_metabox_actions');